package com.ruoyi.aitcommon.utils;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.aitcommon.vo.RuntimeVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class LogUtils {
//	静态方法里边如何使用spring的注入对象
//	参考文档 : https://blog.csdn.net/Jarbein/article/details/117857580
	private static LogUtils logUtils;
	@Autowired
	RuntimeManager runtimeManager;
	@PostConstruct
	public void init(){
		logUtils = this;
		logUtils.runtimeManager = this.runtimeManager;
	}

	private static final Logger log = LoggerFactory.getLogger("ait-common");
	private static final Logger sqllog = LoggerFactory.getLogger("ait-sql");
	private static final Logger httplog = LoggerFactory.getLogger("ait-http");
	private static final Logger perfLog = LoggerFactory.getLogger("ait-perf");
	
	public static Logger getLogger() {
		return log;
	}
	
	public static void info(String msg) {
//		log.info(msg);
		info(msg,new Object[]{});
	}
	
	/**
	 * 日志
	 * @param msg 消息
	 * @param arg 参数值,格式:("XX消息:{}",arg)
	 */
	public static void info(String msg,Object arg) {
//		log.info(msg, arg);
		info(msg,new Object[]{arg});
	}
	/**
	 * 日志
	 * @param msg 消息
	 * @param args 参数值,格式:("XX消息:{},{}",arg1,arg2)
	 */
	public static void info(String msg,Object... args) {
		log.info(msg,args);
//		记录到RunTimeManager中
		if(logUtils!=null&&logUtils.runtimeManager!=null)
			logUtils.runtimeManager.appendLog(RuntimeVO.LOGINFO,msg,args);
	}
		
	public static void debug(String msg) {
		log.debug(msg);
	}
	public static void debug(String msg,Object arg) {
		log.debug(msg,arg);
	}
	public static void debug(String msg,Object... args) {
		log.debug(msg,args);
	}
	
	public static void error(String msg) {
		error(msg,new Object[]{});
	}
	public static void error(String msg,Object arg) {
		error(msg,new Object[]{arg});
	}
	public static void error(String msg,Object... args) {
		log.error(msg,args);
		if(logUtils!=null&&logUtils.runtimeManager!=null)
			logUtils.runtimeManager.appendLog(RuntimeVO.LOGERR,msg,args);
	}
	public static void error(String msg,Throwable ex) {
		log.error(msg, ex);
		if(logUtils!=null&&logUtils.runtimeManager!=null)
			logUtils.runtimeManager.appendLog(msg,ex);
	}
	
	public static void warn(String msg) {
		warn(msg,new Object[]{});
	}

	/**
	 * 消息
	 * @param msg
	 * @param arg 参数值,格式:("XX消息:{},{}",arg1,arg2)
	 */
	public static void warn(String msg,Object arg) {
		warn(msg,new Object[]{arg});
	}

	/**
	 * 警告消息
	 * @param msg
	 * @param args 参数值,格式:("XX消息:{},{}",arg1,arg2)
	 */
	public static void warn(String msg,Object... args) {
		log.warn(msg, args);
		if(logUtils!=null&&logUtils.runtimeManager!=null)
			logUtils.runtimeManager.appendLog(RuntimeVO.LOGWARN,msg,args);
	}
	
	public static void sqlDBDebug(String sql, DBUtils db) {
		sqllog.debug("SQL执行===============");
		sqllog.debug("FLowID:[{}] FlowCode:[{}] FlowName:[{}]"
				,FlowUtils.getFlowID(),FlowUtils.getFlowCode(),FlowUtils.getFlowName());
		sqllog.debug("DB链接：" + db.url);
		sqllog.debug("SQL语句:" + sql);
		sqllog.debug("");
	}
	
	public static void sqlDebug(String msg) {
		sqllog.debug(msg);
	}
	
	public static void sqlDebug(String msg,Object... args) {
		sqllog.debug(msg,args);
	}
	
	public static void httpDebug(String msg) {
		httplog.debug(msg);
	}

	public static void httpDebug(String msg,Object arg) {
		httplog.debug(msg,arg);
	}
	
	public static void httpDebug(String msg,Object... args) {
		httplog.debug(msg,args);
	}

	public static void httpInfo(String msg){
		httpInfo(msg,new Object[]{});
	}

	/**
	 * http日志
	 * @param msg 消息
	 * @param args 参数值,格式:("XX消息:{},{}",arg1,arg2)
	 */
	public static void httpInfo(String msg,Object ...args){
		httplog.info(msg,args);
		//		记录到RunTimeManager中
		if(logUtils!=null&&logUtils.runtimeManager!=null)
			logUtils.runtimeManager.appendLog(RuntimeVO.LOGINFO,msg,args);
	}

	public static void perfWarn(String msg){
		perfLog.warn(msg,new Object[]{});
	}
	/**
	 * 性能日志
	 * @param msg 消息
	 * @param args 参数值,格式:("XX消息:{},{}",arg1,arg2)
	 */
	public static void perfWarn(String msg,Object ...args){
		perfLog.warn(StrUtil.format("FLowID:[{}] FlowCode:[{}] FlowName:[{}]\r\n"
				,FlowUtils.getFlowID(),FlowUtils.getFlowCode(),FlowUtils.getFlowName()));
		perfLog.warn(msg,args);
		if(logUtils!=null&&logUtils.runtimeManager!=null)
			logUtils.runtimeManager.appendLog(RuntimeVO.LOGWARN,msg,args);
	}
}
